/****************************************************************
 * <strong>MFRedirectView</strong>
 *
 * <p>
 * MFRedirectView
 * </p>
 *
 * @author AndyZhang 2013
 * @version 0.1
 * ToDo:2013-3-3
 ***************************************************************/

package com.thinkwide.data.mvc.view;

import com.thinkwide.data.security.MFJSEncode;
import com.thinkwide.data.core.MFString;
import com.thinkwide.data.mvc.core.IMFView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class MFRedirectView implements IMFView {
    private String url;

    public MFRedirectView(String url) {
        this.url = url;
    }

    public String getContentType() {

        return null;
    }

    public void render(Map<String, Object> map, HttpServletRequest request,
                       HttpServletResponse response) throws Exception {
        String param = "";

        if (map != null)
            for (Map.Entry<String, Object> item : map.entrySet()) {
                String value = item.getValue() == null ? "" : item.getValue().toString();
                if (MFString.isNullOrEmpty(param))
                    param = item.getKey() + "=" + MFJSEncode.UrlEncode(value, "utf-8");
                else
                    param = param + "&" + item.getKey() + "=" + MFJSEncode.UrlEncode(value, "utf-8");

            }

        if (MFString.notNullOrEmpty(param)) {
            if (url.indexOf('?') > 0)
                url = url + "&" + param;
            else
                url = url + "?" + param;
        }

        response.sendRedirect(url);
    }

}
